package com.study.d024chart;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ScatterChart;
import javafx.scene.chart.StackedBarChart;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.control.Tooltip;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**Chart接口 ,   XYChart 子接口 的实现类ScatterChart
 * ScatterChart 区域图
*@description
*@author xtayhame
*date 2020年3月18日
*version
*since v1.0
*/
public class AreaChartMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();
		HBox hBox = new HBox(10);

		AreaChart<String, Number> lineChart1 = getView1();
		hBox.getChildren().addAll(lineChart1);
		anchorPane.getChildren().add(hBox);
		stage.setScene(new Scene(anchorPane, 900, 500));
		stage.show();

		//给每个数据节点添加 提示
		ObservableList<XYChart.Series<String,Number>> datas=lineChart1.getData();
		if(datas !=null && datas.size() > 0){
		    for(XYChart.Series<String,Number> xy:datas){
		    	ObservableList<XYChart.Data<String, Number>> nodes=xy.getData();
		    	if(nodes !=null && nodes.size() > 0){
		    		for(XYChart.Data<String, Number>  n:nodes){
		    			Node node= n.getNode();
		    			Tooltip tip = new Tooltip("产量："+n.getYValue());
		    			Tooltip.install(node, tip);

		    			//给数据节点添加事件
		    			node.setOnMouseClicked(new EventHandler<MouseEvent>() {
							@Override
							public void handle(MouseEvent event) {
								System.out.println("-" + n.getXValue() +"," + n.getYValue());
							}
						});
		    		}
		    	}
		    }
		}
	}

	public AreaChart<String, Number> getView1(){
		CategoryAxis x =new CategoryAxis();
		x.setLabel("月份");

		NumberAxis  y = new NumberAxis();
		y.setLabel("产量");

		ObservableList<XYChart.Series<String,Number>> datas = FXCollections.observableArrayList();

		XYChart.Series<String, Number> xy1 = new XYChart.Series<String, Number>();
		ObservableList<Data<String, Number>> data1 = FXCollections.observableArrayList();
		Data<String, Number> d1 = new Data<String, Number>("一月",23);
		Data<String, Number> d2 = new Data<String, Number>("二月",43);
		Data<String, Number> d3 = new Data<String, Number>("三月",53);
		data1.addAll(d1,d2,d3);
		xy1.setData(data1);
		xy1.setName("生产线一");


		d1.setExtraValue(23);

		XYChart.Series<String, Number> xy2 = new XYChart.Series<String, Number>();
		ObservableList<Data<String, Number>> data2 = FXCollections.observableArrayList();
		Data<String, Number> d4 = new Data<String, Number>("一月",83);
		Data<String, Number> d5 = new Data<String, Number>("二月",51);
		Data<String, Number> d6 = new Data<String, Number>("三月",29);
		data2.addAll(d4,d5,d6);
		xy2.setData(data2);
		xy2.setName("生产线二");


		datas.add(xy1);
		datas.add(xy2);

		AreaChart<String,Number> lineChart=new AreaChart(x, y);
		lineChart.setData(datas);


		lineChart.setTitle("全年产量表一");
		return lineChart;
	}



}
